<?php

require_once('_Core.php');

class SilkCore_LayoutMeta_MetaDescription extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Meta Description';
    }

    public function getInputType(){
	return Silk::INPUT_TEXTAREA;
    }

}

class SilkCore_LayoutMeta_MetaKeywords extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Meta Keywords';
    }

    public function getInputType(){
	return Silk::INPUT_TEXT;
    }

}

class SilkCore_LayoutMeta_MetaTitle extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Meta Title';
    }

    public function getInputType(){
	return Silk::INPUT_TEXT;
    }

}

class SilkCore_LayoutMeta_CustomPhp extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Custom PHP';
    }

    public function getInputType(){
	return Silk::INPUT_TEXTAREA;
    }

}

class SilkCore_LayoutMeta_CustomHead extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Custom Head';
    }

    public function getInputType(){
	return Silk::INPUT_TEXTAREA;
    }
}

class SilkCore_LayoutMeta_DocType extends Silk_Module_Type_Meta {

    public function getName(){
	return 'Doctype';
    }

    public function getInputType(){
	return Silk::INPUT_SELECT;
    }

    public function getValues(){
	return array(
	    'XHTML 1.1'			=> SILKCORE_DOCTYPE_XHTML11,
	    'XHTML 1.0 Strict' 		=> SILKCORE_DOCTYPE_XHTML10_STRICT,
	    'XHTML 1.0 Transitional' 	=> SILKCORE_DOCTYPE_XHTML10_TRANSITIONAL,
	    'XHTML 1.0 Frameset' 	=> SILKCORE_DOCTYPE_XHTML10_FRAMESET,
	    'HTML 4.01 Strict' 		=> SILKCORE_DOCTYPE_HTML4_STRICT,
	    'HTML 4.01 Transititional'	=> SILKCORE_DOCTYPE_HTML4_TRANSITIONAL,
	    'HTML 4.01 Frameset' 	=> SILKCORE_DOCTYPE_HTML4_FRAMESET
	);
    }
}

class SilkCore_Type_Layout extends SilkCore_Type_Base {


    public function __construct(){
	$this->_meta = array(
	    new SilkCore_LayoutMeta_CustomHead(),
	    new SilkCore_LayoutMeta_CustomPhp(),
	    new SilkCore_LayoutMeta_MetaDescription(),
	    new SilkCore_LayoutMeta_MetaKeywords(),
	    new SilkCore_LayoutMeta_MetaTitle(),
	    new SilkCore_LayoutMeta_DocType()
	);
    }

    public function getName(){
	return 'Layout Template';
    }

    public function getSlug(){
	return 'layout';
    }

    public function getDescription(){
	return 'Layout Template';
    }

    public function render(Silk_Object $pLayout=null, $pAction=null, array $pVars=array()){
	if ($pAction instanceof Silk_Template)
	    return parent::render($pLayout, $pAction, $pVars);
	else if ($pAction){
	    $view = $this->prepare($pLayout, $pVars);

	    $action = strtolower($pAction);
	    switch ($action){
		case 'admin':
		    return $this->_admin($view);
		case 'create':
		    return $this->_create($view, $pLayout);
		case 'view':
		    return $this->_view($view, $pLayout);
		case 'edit':
		    return $this->_edit($view, $pLayout);
		case 'delete':
		    return $this->_delete($view, $pLayout);
	    }

	} else {
	    $view = $this->prepare($pLayout, $pVars);
	    return $this->renderString($view, $pLayout->content);
	}
    }

    protected function prepare(Silk_Object $pLayout=null, array $pVars=array()){
	$view = $this->getViewEngine();

	if ($pLayout){
	    $view->assign('layout', $pLayout);
	    $view->assign('object', $pLayout);
	}

	foreach ($pVars as $key => $val){
	    $view->assign($key, $val);
	}

	return $view;
    }


    public function _view(Zend_View_Interface $pView, Silk_Object $pLayout){
	$page = $pView->page;
	if ($page){
	    $page_doctype = $page->getMetaValue('Doctype');

	    $DoctypeMeta = new SilkCore_LayoutMeta_Doctype();

	    if ($page_doctype){
		foreach ($DoctypeMeta->getValues() as $key => $val){
		    if ($key == $page_doctype)
			$doctype = $val;
		}
	    } else {
		$layout_doctype = $pLayout->getMetaValue('Doctype');

		if ($layout_doctype){
		    foreach ($DoctypeMeta->getValues() as $key => $val){
			if ($key == $layout_doctype)
			    $doctype = $val;
		    }
		} else
		    $doctype = SILKCORE_DOCTYPE_XHTML10_TRANSITIONAL;
	    }

	    $before  = $doctype . "\n";
	    $before .= '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
	    $before .= '	<head>' ."\n";

	    $page_metaTitle = $page->getMetaValue('Meta Title');
	    if ($page_metaTitle){
		$before .= '		<title>' . $page_metaTitle . '</title>' . "\n";
	    } else {
		$layout_metaTitle = $pLayout->getMetaValue('Meta Title');
		if ($layout_metaTitle){
		    $before .= '		<title>' . $layout_metaTitle . '</title>' . "\n";
		}
	    }

	    $layout_customHead = $pLayout->getMetaValue('Custom Head');
	    if ($layout_customHead){
		try {
		    $before .= $this->renderString($pView, $layout_customHead);
		} catch (Exception $x){
		    logexception($x);
		    $before .= '<!-- There was a problem rendering the layout custom head -->' . "\n";
		}
	    }

	    $page_customHead = $page->getMetaValue('Custom Head');
	    if ($page_customHead){
		try {
		    $before .= $this->renderString($pView, $page_customHead);
		} catch (Exception $x){
		    logexception($x);
		    $before .= '<!-- There was a problem rendering the page custom head -->' . "\n";
		}
	    }

	    if (Silk::HasIdentity()){
		$user = Silk::GetIdentity();
		if ($user->isAdministrator()){

		    $pView->this->appendCss(array('jquery-ui-theme'));
		    $pView->this->appendStylesheet($this, 'css/core.manage.css');

		    $pView->this->appendScript($this, 'js/jquery.min.js');
		    $pView->this->appendScript($this, 'js/jquery-ui.min.js');
		    $pView->this->appendScript($this, 'js/jquery.entwine.js');
		    $pView->this->appendScript($this, 'js/jquery.form.js');
		    $pView->this->appendScript($this, 'js/jquery.blockUI.js');

		    $pView->this->appendScript($this, 'js/core.manage.js');
		    $pView->this->appendScript($this, 'js/core.page.js');
		}
	    }

 	    $before .= '<tal:block tal:content="structure this/appendCss"/>' . "\n";
	    $before .= '<tal:block tal:content="structure this/appendJs"/>' . "\n";
	    $before .= '<tal:block tal:content="structure this/renderAssets"/>' . "\n";


	    $before .= '	</head>' . "\n";
	    $before .= '	<body id="' . $page->slug . '">' . "\n";

	    $after   = '	</body>' . "\n";
	    $after	.= '</html>';

	    $document = $before . $pLayout->content . $after;

	    Silk::debug($document);
	    try{
		$output = $this->renderString($pView, $document);
	    } catch (Exception $x){
		$output = '<div class="silk-template-error">Failed to render layout ' . $pLayout->name .'. Please contact your Administrator</div>';
		logexception($x);
		if (Silk::HasIdentity()){
		    $user = Silk::GetIdentity();
		    if ($user->isAdministrator()) $output .= '<div class="silk-template-error">' . $x->getMessage() . '</div>';
		}


	    }
// 	    if (function_exists('tidy_repair_string')){
// 		$output = tidy_repair_string($output, $this->getTidyConfig());
// 	    }

	    return $output;
	} else
	    throw new Silk_Exception('No page to render');
    }

    protected function _create(Zend_View_Interface $pView, Silk_Object $pLayout){

	$pView->this->appendStylesheet($this, 'css/core.manage.css');

	return $this->renderTemplate($pView, 'edit-layout');

    }

    protected function _edit(Zend_View_Interface $pView, Silk_Object $pLayout){

	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	return $this->renderTemplate($pView, 'edit-layout');

    }

    protected function _admin(Zend_View_Interface $pView){
	if (array_key_exists('sort', $_REQUEST)){
	    $sort = $_REQUEST['sort'];
	} else
	    $sort = 'object_id';

	if (array_key_exists('order', $_REQUEST)){
	    $order = $_REQUEST['order'];
	} else
	    $order = 'ASC';

	if (array_key_exists('limit', $_REQUEST)){
	    $limit = $_REQUEST['limit'];
	} else
	    $limit = 50;

	if (array_key_exists('page', $_REQUEST)){
	    $page = $_REQUEST['page'];
	} else
	    $page = 1;

	$paginator = Silk::getObjectPaginator($this->getSlug(), array(), $sort, $order);

	$paginator->setItemCountPerPage($limit);
	$paginator->setCurrentPageNumber($page);

	$Types = new Silk_Types();
	$type = $Types->fetchBySlug($this->getSlug(), true);

	$pView->moduletype = $this;
	$pView->type = $type;

	$pView->layouts = $paginator;
	$pView->limit = $limit;
	$pView->sort  = $sort;
	$pView->order = $order;
	$pView->this->appendStylesheet($this, 'css/core.manage.css');
	return $this->renderTemplate($pView, 'layout-admin');
    }
}

?>